
package com.hlkj.pay.service.common.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.LocalContext;
import com.hlkj.framework.common.pojo.LocalRequest;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.dto.LocalAdminUserRequest;
import com.hlkj.pay.dto.LocalMerchantRequest;
import com.hlkj.pay.dto.LocalUserRequest;
import com.hlkj.pay.dto.common.ExportRecordDto;
import com.hlkj.pay.dto.common.ExportRecordQueryDto;
import com.hlkj.pay.infrastructure.mapper.common.ExportRecordMapper;
import com.hlkj.pay.infrastructure.model.common.ExportRecordDO;
import com.hlkj.pay.service.common.IExportRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author HlpayTeam
 * @date 2024/11/20 13:35
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ExportRecordServiceImpl implements IExportRecordService {

    private final ExportRecordMapper exportRecordMapper;

    @Override
    public void add(ExportRecordDO exportRecordDO) {
        exportRecordMapper.insert(exportRecordDO);
    }

    @Override
    public void update(ExportRecordDO exportRecordDO) {
        exportRecordMapper.updateById(exportRecordDO);
    }

    @Override
    public PageResult<ExportRecordDO> queryPage(ExportRecordQueryDto exportRecordQueryDto) {
        return exportRecordMapper.selectPage(exportRecordQueryDto, buildWrapper(exportRecordQueryDto));
    }

    LambdaQueryWrapper<ExportRecordDO> buildWrapper(ExportRecordQueryDto exportRecordQueryDto) {
        filterUser(exportRecordQueryDto);
        LambdaQueryWrapperX<ExportRecordDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(ExportRecordDO::getStatus, exportRecordQueryDto.getStatus());
        queryWrapper.eqIfPresent(ExportRecordDO::getCreatedBy, exportRecordQueryDto.getCreateBy());
        return queryWrapper;
    }

    void filterUser(ExportRecordQueryDto exportRecordQueryDto) {
        LocalRequest localRequest = LocalContext.get();
        if (localRequest == null) {
            return;
        }
        exportRecordQueryDto.setCreateBy(localRequest.getUserId());
    }
}
